<script lang="ts" setup>
import { reactive, ref } from "vue"
import { useRouter } from "vue-router"
import { ElMessage, type FormInstance, type FormRules, ElNotification } from "element-plus"
import { User, Lock, Key, Picture, Loading } from "@element-plus/icons-vue"
import ThemeSwitch from "@/components/ThemeSwitch/index.vue"
import Owl from "./components/Owl.vue"
import AnimatedLogo from "./components/AnimatedLogo.vue"
import { useFocus } from "./hooks/useFocus"
import { loginAPI } from "@/api/admin/user"
import { useUserStore } from "@/store/modules/user"
import type { LoginResponseData } from "@/types/admin/user"

const router = useRouter()
const { isFocus, handleBlur, handleFocus } = useFocus()

// 用户信息仓库
const userStore = useUserStore()

/** 登录表单元素的引用 */
const loginFormRef = ref<FormInstance | null>(null)

/** 登录按钮 Loading */
const loading = ref(false)
/** 验证码图片 URL */
const codeUrl = ref("")
/** 登录表单数据 */
const loginFormData: any = reactive({
  username: "",
  password: "",
  code: "V3Admin"
})
/** 登录表单校验规则 */
const loginFormRules: FormRules = {
  username: [{ required: true, message: "请输入用户名", trigger: "blur" }],
  password: [
    { required: true, message: "请输入密码", trigger: "blur" },
    { min: 6, max: 16, message: "长度在 6 到 16 个字符", trigger: "blur" }
  ],
  code: [{ required: true, message: "请输入验证码", trigger: "blur" }]
}

/** 登录逻辑 */
const handleLogin = () => {
  loginFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      // 开启按钮加载
      loading.value = true
      // 调用接口发送请求
      const res: LoginResponseData = await loginAPI({
        username: loginFormData.username,
        password: loginFormData.password
      })
      // 关闭按钮加载
      loading.value = false
      // 判断登录状态
      console.log(res)
      if (res.code === 200) {
        // 保存token
        userStore.setUserToken(res.token)
        ElMessage.success("登录成功")

        // 跳转到后台首页
        router.push("/dashboard")
      } else {
        ElMessage.error("用户名或密码错误")
      }
      console.log(res)
    } else {
      console.error("表单校验不通过")
    }
  })
}

// 返回前台
const goBackHome = () => {
  router.push({ path: "/" })
}

//修改密码
const goUpdataPassWord = () => {

}
</script>

<template>
  <div class="login-container">
    <ThemeSwitch class="theme-switch" />
    <div class="logo-section">
      <AnimatedLogo />
    </div>
    <div class="owl-container">
      <Owl :close-eyes="isFocus" />
    </div>
    <div class="login-card">
      <div class="content">
        <el-form ref="loginFormRef" :model="loginFormData" :rules="loginFormRules" @keyup.enter="handleLogin">
          <el-form-item prop="username">
            <el-input v-model.trim="loginFormData.username" placeholder="用户名" type="text" tabindex="1" :prefix-icon="User" size="large" />
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              v-model.trim="loginFormData.password"
              placeholder="密码"
              type="password"
              tabindex="2"
              :prefix-icon="Lock"
              size="large"
              show-password
              @blur="handleBlur"
              @focus="handleFocus"
            />
          </el-form-item>
          <el-button class="login-btn" :loading="loading" type="primary" size="large" @click.prevent="handleLogin">登 录</el-button>
          <div class="help">
            <span @click="goBackHome">返回前台</span>
            <span @click="goUpdataPassWord">忘记密码？</span>
          </div>
        </el-form>
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.login-container {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  width: 100%;
  min-height: 100vh;
  background: var(--gradient-primary);
  position: relative;
  overflow: hidden;

  // 添加背景动画粒子效果
  &::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background:
      radial-gradient(circle at 20% 80%, rgba(120, 119, 198, 0.3) 0%, transparent 50%),
      radial-gradient(circle at 80% 20%, rgba(255, 255, 255, 0.15) 0%, transparent 50%),
      radial-gradient(circle at 40% 40%, rgba(120, 119, 198, 0.2) 0%, transparent 50%);
    animation: floatBubbles 20s ease-in-out infinite;
  }

  .theme-switch {
    position: fixed;
    top: 5%;
    right: 5%;
    cursor: pointer;
    z-index: 100;
  }

  .logo-section {
    position: relative;
    z-index: 20;
    margin-top: 20px;
    animation: logoSlideIn 1s ease-out;
  }

  .owl-container {
    position: relative;
    z-index: 15;
    animation: owlSlideIn 1.2s ease-out 0.5s both;
  }

  .login-card {
    width: 480px;
    max-width: 90%;
    border-radius: 25px;
    box-shadow:
      0 25px 45px rgba(0, 0, 0, 0.1),
      0 0 0 1px rgba(255, 255, 255, 0.05);
    background: rgba(255, 255, 255, 0.95);
    backdrop-filter: blur(20px);
    border: 1px solid rgba(255, 255, 255, 0.2);
    overflow: hidden;
    position: relative;
    z-index: 10;
    animation: cardSlideIn 0.8s ease-out;

    // 卡片内部光效
    &::before {
      content: '';
      position: absolute;
      top: 0;
      left: -100%;
      width: 100%;
      height: 100%;
      background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.4), transparent);
      transition: left 0.5s;
    }

    &:hover::before {
      left: 100%;
    }

    .content {
      padding: 30px 50px 50px 50px;

      :deep(.el-input-group__append) {
        padding: 0;
        overflow: hidden;
        .el-image {
          width: 100px;
          height: 40px;
          border-left: 0px;
          user-select: none;
          cursor: pointer;
          text-align: center;
        }
      }

      :deep(.el-input) {
        .el-input__wrapper {
          border-radius: 12px;
          transition: all 0.3s ease;
          box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);

          &:hover {
            box-shadow: 0 4px 12px rgba(102, 126, 234, 0.2);
          }

          &.is-focus {
            box-shadow: 0 4px 12px rgba(102, 126, 234, 0.3);
          }
        }
      }

      .el-button {
        width: 100%;
        margin-top: 20px;
        border-radius: 12px;
      }
    }
  }
}

// 添加背景动画
@keyframes floatBubbles {
  0%, 100% {
    transform: translateY(0px) translateX(0px);
  }
  33% {
    transform: translateY(-30px) translateX(20px);
  }
  66% {
    transform: translateY(-20px) translateX(-20px);
  }
}

@keyframes cardSlideIn {
  0% {
    opacity: 0;
    transform: translateY(50px) scale(0.9);
  }
  100% {
    opacity: 1;
    transform: translateY(0px) scale(1);
  }
}

@keyframes logoSlideIn {
  0% {
    opacity: 0;
    transform: translateY(-30px) scale(0.8);
  }
  100% {
    opacity: 1;
    transform: translateY(0px) scale(1);
  }
}

@keyframes owlSlideIn {
  0% {
    opacity: 0;
    transform: translateY(50px) scale(0.7) rotate(-10deg);
  }
  60% {
    transform: translateY(-10px) scale(1.05) rotate(5deg);
  }
  100% {
    opacity: 1;
    transform: translateY(0px) scale(1) rotate(0deg);
  }
}

.login-btn {
  overflow: hidden;
  position: relative;
  text-decoration: none;
  padding: 15px 30px;
  color: #fff;
  border-radius: 12px;
  background: var(--gradient-primary);
  border: none;
  transition: all 0.3s ease;
  text-align: center;
  font-size: 16px;
  font-weight: 600;
  letter-spacing: 1px;
  box-shadow: 0 8px 25px rgba(102, 126, 234, 0.3);

  &::before {
    content: "";
    position: absolute;
    top: 0;
    left: -100%;
    width: 100%;
    height: 100%;
    background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.3), transparent);
    transform: skewX(-45deg);
    transition: left 0.6s;
  }

  &:hover {
    transform: translateY(-2px);
    box-shadow: 0 12px 35px rgba(102, 126, 234, 0.4);

    &::before {
      left: 100%;
    }
  }

  &:active {
    transform: translateY(0px);
    box-shadow: 0 5px 15px rgba(102, 126, 234, 0.3);
  }
}

.help {
  display: flex;
  justify-content: space-between;
  font-size: 14px;
  color: #666;
  margin-top: 20px;
  padding: 0 20px;

  span {
    cursor: pointer;
    transition: all 0.3s ease;
    padding: 5px 10px;
    border-radius: 8px;

    &:hover {
      color: #667eea;
      background: rgba(102, 126, 234, 0.1);
      transform: translateY(-1px);
    }
  }
}
</style>
